package com.avic.rong.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.userOrder.bean.CaiLikeBean;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XinEr on 2019/7/10.
 * Describe:
 */
public class MineLikeAdapter extends RecyclerView.Adapter<MineLikeAdapter.LikeViewHolder> {
    private Context context;
    private ArrayList<CaiLikeBean> listData;

    public MineLikeAdapter(Context context, ArrayList<CaiLikeBean> listData) {
        this.context = context;
        this.listData = listData;
    }


    @NonNull
    @Override
    public LikeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.mine_like_item, viewGroup, false);
        LikeViewHolder viewHolder = new LikeViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull LikeViewHolder viewHolder, int position) {
        CaiLikeBean caiLikeBean = listData.get(position);
//        GlideUtils.loaderImage(context, caiLikeBean.getImg(), viewHolder.iv_book_photo);
//        Glide.with(context).load(caiLikeBean.getImg()).placeholder(R.mipmap.common_bg).into(viewHolder.iv_book_photo);
        if (!StringUtils.isEmpty(caiLikeBean.getImg())) {
            viewHolder.ll_center_tip.setVisibility(View.GONE);
            Glide.with(context).load(caiLikeBean.getImg())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                    .placeholder(R.mipmap.common_bg)
                    .override(DensityUtils.dp2px(context, 135), DensityUtils.dip2px(context, 135))
                    .error(R.mipmap.common_bg)
//                    .into(viewHolder.iv_book_photo);
                    .listener(new MyRequestListener(viewHolder, caiLikeBean)).submit();
        } else {
            viewHolder.ll_center_tip.setVisibility(View.VISIBLE);
            Glide.with(context).load(caiLikeBean.getImg())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                    .override(DensityUtils.dp2px(context, 135), DensityUtils.dip2px(context, 135))
                    .placeholder(R.mipmap.common_bg).error(R.mipmap.common_bg)
//                    .into(viewHolder.iv_book_photo);
                    .listener(new MyRequestListener(viewHolder, caiLikeBean)).submit();
        }


        viewHolder.tv_book_name.setText(caiLikeBean.getTitle());


        viewHolder.tv_standard_no.setText(caiLikeBean.getNo());
        viewHolder.tv_standard_cat.setText(caiLikeBean.getCatName());

        if (!StringUtils.isEmpty(caiLikeBean.getAttrShow())) {
            viewHolder.tv_standard_status.setVisibility(View.VISIBLE);
            viewHolder.tv_standard_status.setText(caiLikeBean.getAttrShow());
        } else {
            viewHolder.tv_standard_status.setVisibility(View.GONE);
        }

        SpannableString string = changTVsize(caiLikeBean.getPrice());
        viewHolder.tv_book_price.setText("￥" + string);
        viewHolder.itemView.setOnClickListener(new MyOnClickListener(caiLikeBean));
    }

    class MyRequestListener implements RequestListener<Drawable> {

        LikeViewHolder viewHolder;
        CaiLikeBean caiLikeBean;

        public MyRequestListener(LikeViewHolder viewHolder, CaiLikeBean caiLikeBean) {
            this.viewHolder = viewHolder;
            this.caiLikeBean = caiLikeBean;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
            if (StringUtils.isEmpty(caiLikeBean.getCatName()) && StringUtils.isEmpty(caiLikeBean.getNo())) {
                viewHolder.ll_center_tip.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.ll_center_tip.setVisibility(View.VISIBLE);
            }
//            viewHolder.iv_book_photo.setBackgroundResource(R.mipmap.common_bg);
            viewHolder.iv_book_photo.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.common_bg));
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
            viewHolder.iv_book_photo.setImageDrawable(drawable);
            viewHolder.ll_center_tip.setVisibility(View.GONE);
            return false;
        }
    }

    @Override
    public int getItemCount() {
        if (listData.size() >= 6) {
            return 6;
        }
        return listData.size();
    }


    class MyOnClickListener implements View.OnClickListener {


        CaiLikeBean caiLikeBean;

        public MyOnClickListener(CaiLikeBean caiLikeBean) {
            this.caiLikeBean = caiLikeBean;
        }

        @Override
        public void onClick(View v) {
            context.startActivity(DetailProductActivity.getIntent(context, String.valueOf(caiLikeBean.getGoodsId())));
        }
    }


    public class LikeViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_book_photo)
        ImageView iv_book_photo;
        @BindView(R.id.tv_book_name)
        TextView tv_book_name;
        @BindView(R.id.tv_book_price)
        TextView tv_book_price;

        @BindView(R.id.rl_standard_no)
        RelativeLayout rl_standard_no;
        @BindView(R.id.tv_standard_status)
        TextView tv_standard_status;
        @BindView(R.id.tv_standard_page)
        TextView tv_standard_page;
        @BindView(R.id.tv_standard_cat)
        TextView tv_standard_cat;
        @BindView(R.id.tv_standard_no)
        TextView tv_standard_no;
        @BindView(R.id.ll_center_tip)
        LinearLayout ll_center_tip;//中间布局


        public LikeViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static SpannableString changTVsize(String value) {
        SpannableString spannableString = new SpannableString(value);
        if (value.contains(".")) {
            spannableString.setSpan(new RelativeSizeSpan(0.6f), value.indexOf("."), value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

}
